﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Snippet_saver
{
    
    public partial class Form1 : Form
    {

       public string fileselected = "";
        public Form1()
        {
            InitializeComponent();
        }

        void button2_Click(object sender, EventArgs e)
        {
            try
            {
                listBox1.Items.Clear();
                foreach (string fileinput in Directory.GetFiles("./data"))
                {

                    listBox1.Items.Add(fileinput.Replace("./data", ""));
                }
            }
            catch (System.IO.DirectoryNotFoundException)
            {

                MessageBox.Show("data directory not found, creating one now..");
                Directory.CreateDirectory("data");
                statuslabel.Text = "Data directory created.";
            }
            catch (Exception ex)
            {
 MessageBox.Show("You fucked up didn't you. >:|\nI couldn't open the folder, but it IS there...\nMore info below:\n\n " + ex);
 statuslabel.Text = "Error occured while listing files.";
            }
            finally
            {
                statuslabel.Text = "List refreshed";         
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            string openfilethingy = "./data" + listBox1.SelectedItem.ToString();
            try
            {
                
                richTextBox1.LoadFile(openfilethingy,RichTextBoxStreamType.PlainText);
                statuslabel.Text = "File loaded.";
            }
            catch (Exception ex)
            {
                MessageBox.Show("error occured, is the file format compatible?\nmore info:\n" + ex.ToString());
                statuslabel.Text = "Error occured while loading file.";
            }


        }

        private void save_button_Click(object sender, EventArgs e)
        {
            try
            {
            richTextBox1.SaveFile("./data" + listBox1.SelectedItem.ToString(),RichTextBoxStreamType.PlainText);
         statuslabel.Text = "File succesfully saved.";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error has occured, see below.\n\n" + ex.ToString());
                statuslabel.Text = "Error occured while saving file.";
            }
            finally
            {
                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            createfile createfile = new createfile();
            createfile.ShowDialog();
            statuslabel.Text = "New file created";
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(listBox2.SelectedIndex > 0)
           
            {
                if(MessageBox.Show("Are you sure you want to delete " + listBox2.SelectedItem.ToString() + "?"," ",MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                File.Delete("./data/" + listBox2.SelectedItem.ToString());
                statuslabel.Text = "File deleted.";
                }
                else
                {
                statuslabel.Text = "File NOT deleted.";
                }
            }
            else
            {
                if(MessageBox.Show("Are you sure you want to delete " + listBox1.SelectedItem.ToString() + "?"," ",MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                File.Delete("./data/" + listBox1.SelectedItem.ToString());
                statuslabel.Text = "File deleted.";
                }
                else
                {
                statuslabel.Text = "File NOT deleted.";
                }
            }
    
        }

        private void Searchbutton_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("What you wanna search for?");
                statuslabel.Text = "No search string found";
            }
            else
            {
                listBox2.Items.Clear();
               
                foreach(string founditem in Directory.GetFiles("./data/"))
                    if (founditem.Contains(textBox1.Text))
                    {
                        listBox2.Items.Add(founditem.Replace("./data",""));
                    }
                statuslabel.Text = "Search completed.";
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string openfilethingy = "./data" + listBox2.SelectedItem.ToString();
            try
            {

                richTextBox1.LoadFile(openfilethingy, RichTextBoxStreamType.PlainText);
                statuslabel.Text = "File loaded.";
            }
            catch (Exception ex)
            {
                MessageBox.Show("error occured, is the file format compatible?\nmore info:\n" + ex.ToString());
                statuslabel.Text = "Error occured while loading file.";
            }


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                listBox1.Items.Clear();
                foreach (string fileinput in Directory.GetFiles("./data"))
                {

                    listBox1.Items.Add(fileinput.Replace("./data", ""));
                }
            }
            catch (System.IO.DirectoryNotFoundException)
            {

                MessageBox.Show("data directory not found, creating one now..");
                Directory.CreateDirectory("data");
                statuslabel.Text = "Data directory created.";
            }
            catch (Exception ex)
            {
                MessageBox.Show("You fucked up didn't you. >:|\nI couldn't open the folder, but it IS there...\nMore info below:\n\n " + ex);
                statuslabel.Text = "Error occured while listing files.";
            }
            finally
            {
                statuslabel.Text = "List refreshed";
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listBox2.Items.Clear();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            fileselected = listBox1.SelectedItem.ToString();
           
            Show_big show_big = new Show_big();
            show_big.ShowDialog();

        }

    }
    }

